﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using FlexCell;
using YArchitech.Controls;
using YArchitech.ProgressBar;
using HyMaterialStatistic;

namespace HvacMatieralStatistic
{
	internal class HvacMaterialList : MEPSetDataToGridParent
	{
		public HvacMaterialList(Autodesk.Revit.DB.Document doc, List<Dictionary<int, SatatisticDataInfo>> lstDicElement, string gridName, GridSetInfo gridSetInfo, ref FlexCell.Grid grid)
		{
			this.m_doc = doc;
			this.m_grid = grid;
			this.m_lstDicElems = lstDicElement;
			this.m_strGridName = gridName;
			this.m_gridSetInfo = gridSetInfo;
		}

		public bool GetStatisticGrid()
		{
			if (this.m_lstDicElems.Count <= 0 || this.m_gridSetInfo == null)
			{
				return false;
			}
			this.GetStatisticData();
			this.m_grid.Locked = false;
			if (!this.m_gridSetInfo.TitleDown)
			{
				if (!this.SetDataToGridUp())
				{
					return false;
				}
			}
			else if (!this.SetDataToGridDown())
			{
				return false;
			}
			return true;
		}

		private void GetStatisticData()
		{
			if (this.m_doc == null)
			{
				return;
			}
			int index;
			int index2;
			if (this.m_gridSetInfo.Legend)
			{
				index = 6;
				index2 = 7;
			}
			else
			{
				index = 5;
				index2 = 6;
			}
			ProgressBarForm progressBarForm = new ProgressBarForm(this.m_lstDicElems.Count, "正在初始化材料表中的数据", ProgressBarStyle.Blocks, true, false, false);
			progressBarForm.Show(WindowHelper.HWndRevit);
			int num = 1;
			foreach (Dictionary<int, SatatisticDataInfo> dictionary in this.m_lstDicElems)
			{
				progressBarForm.RefreshProgressBar();
				if (progressBarForm.m_stopTag)
				{
					break;
				}
				if (dictionary.Count > 0)
				{
					Dictionary<int, List<string>> dictionary2 = new Dictionary<int, List<string>>();
					foreach (KeyValuePair<int, SatatisticDataInfo> keyValuePair in dictionary)
					{
						ElementId elementId = new ElementId(keyValuePair.Key);
						this.m_doc.GetElement(elementId);
						int key = -1;
						if (this.ElemExsit(keyValuePair.Value.Name, keyValuePair.Value.ElemParm, dictionary2, out key))
						{
							string text = (Convert.ToDouble(dictionary2[key][index]) + keyValuePair.Value.Amount).ToString("0.00");
							if (text.IndexOf('.') != -1)
							{
								text = text.TrimEnd(new char[]
								{
									'0'
								});
								text = text.Trim(new char[]
								{
									'.'
								});
							}
							dictionary2[key][index] = text.ToString();
							string ps = keyValuePair.Value.Ps;
							string text2 = dictionary2[key][index2];
							if (base.StringIsNumber(ps) && base.StringIsNumber(text2))
							{
								string text3 = (Convert.ToDouble(ps) + Convert.ToDouble(text2)).ToString("0.00");
								if (text3.IndexOf('.') != -1)
								{
									text3 = text3.TrimEnd(new char[]
									{
										'0'
									});
									text3 = text3.Trim(new char[]
									{
										'.'
									});
								}
								dictionary2[key][index2] = text3.ToString();
							}
						}
						else
						{
							List<string> list = new List<string>();
							int num2 = 0;
							list.Insert(num2, keyValuePair.Value.FilterType);
							list.Insert(++num2, num.ToString());
							if (this.m_gridSetInfo.Legend)
							{
								list.Insert(++num2, keyValuePair.Value.FamilyName);
							}
							list.Insert(++num2, keyValuePair.Value.Name);
							list.Insert(++num2, keyValuePair.Value.ElemParm);
							list.Insert(++num2, keyValuePair.Value.Unit);
							string text4 = keyValuePair.Value.Amount.ToString("0.00");
							if (text4.IndexOf('.') != -1)
							{
								text4 = text4.TrimEnd(new char[]
								{
									'0'
								});
								text4 = text4.Trim(new char[]
								{
									'.'
								});
							}
							list.Insert(++num2, text4);
							list.Insert(num2 + 1, keyValuePair.Value.Ps);
							dictionary2.Add(num, list);
							num++;
						}
					}
					this.m_lstDicData.Add(dictionary2);
				}
			}
		}

		private bool ElemExsit(string name, string elemParm, Dictionary<int, List<string>> dicData, out int key)
		{
			int index;
			int index2;
			if (this.m_gridSetInfo.Legend)
			{
				index = 3;
				index2 = 4;
			}
			else
			{
				index = 2;
				index2 = 3;
			}
			foreach (int num in dicData.Keys)
			{
				List<string> list = dicData[num];
				if (name == list[index] && elemParm == list[index2])
				{
					key = num;
					return true;
				}
			}
			key = -1;
			return false;
		}

		private bool SetDataToGridUp()
		{
			if (this.m_lstDicData.Count <= 0 || this.m_grid == null)
			{
				YJKMessageBox.Show("没有统计数据");
				return false;
			}
			int num = -1;
			int num2 = 0;
			int num3 = 0;
			int nNameIndex = 0;
			int nSepcIndex = 0;
			if (this.m_gridSetInfo.Legend)
			{
				num = 2;
				num2 = 6;
				num3 = 7;
				nNameIndex = 3;
				nSepcIndex = 4;
			}
			else
			{
				num2 = 5;
				num3 = 6;
				nNameIndex = 2;
				nSepcIndex = 3;
			}
			this.m_grid.Cell(1, 1).Text = this.m_strGridName;
			this.m_grid.Cell(1, 1).FontSize = this.m_gridSetInfo.TitleFontSize;
			int num4 = 3;
			this.m_grid.Range(num4 + 1, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).DeleteByRow();
			int num5 = 0;
			foreach (Dictionary<int, List<string>> dictionary in this.m_lstDicData)
			{
				num5 += dictionary.Count;
			}
			this.m_grid.Rows = num4 + num5;
			int num6 = 1;
			foreach (Dictionary<int, List<string>> dictionary2 in this.m_lstDicData)
			{
				List<List<int>> list = base.GroupingDicData(nNameIndex, dictionary2);
				for (int i = 0; i < list.Count; i++)
				{
					List<int> list2 = list[i];
					base.SortData(nSepcIndex, dictionary2, ref list2);
					foreach (int key in list2)
					{
						List<string> list3 = dictionary2[key];
						if (list3.Count > 0)
						{
							int j = 1;
							while (j < list3.Count)
							{
								if (j == 1)
								{
									this.m_grid.Cell(num4, j).Text = num6.ToString();
									goto IL_30F;
								}
								if (j == num)
								{
									string text = list3[j];
									string legendByFamilyName = FamilyLegend.GetLegendByFamilyName(this.m_doc, text);
									text = Path.GetFileName(legendByFamilyName);
									try
									{
										this.m_grid.Images.Add(legendByFamilyName, text);
										if (this.m_grid.Images.Item(text) == null)
										{
											goto IL_325;
										}
										this.m_grid.Cell(num4, j).SetImage(text);
										goto IL_30F;
									}
									catch
									{
										goto IL_325;
									}
								}
								if (j == num2)
								{
									this.m_grid.Cell(num4, j).Text = Math.Ceiling(Convert.ToDouble(list3[j]) * (1.0 + (double)this.m_gridSetInfo.Margin / 100.0)).ToString("0");
									goto IL_30F;
								}
								if (j != num3)
								{
									this.m_grid.Cell(num4, j).Text = list3[j];
									goto IL_30F;
								}
								if (base.StringIsNumber(list3[j]))
								{
									this.m_grid.Cell(num4, j).Text = Convert.ToDouble(list3[j]).ToString("0.00") + "㎡";
									goto IL_30F;
								}
								this.m_grid.Cell(num4, j).Text = list3[j];
								goto IL_30F;
								IL_325:
								j++;
								continue;
								IL_30F:
								this.m_grid.Cell(num4, j).Alignment = AlignmentEnum.CenterCenter;
								goto IL_325;
							}
							num4++;
							num6++;
						}
					}
				}
			}
			this.m_grid.Range(1, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).WrapText = true;
			this.m_grid.Range(2, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).FontName = this.m_gridSetInfo.FontName;
			this.m_grid.Range(2, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).FontSize = this.m_gridSetInfo.FontSize;
			for (int k = 1; k < this.m_grid.Rows; k++)
			{
				this.m_grid.Row(k).AutoFit();
			}
			this.m_grid.Range(1, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).set_Borders(EdgeEnum.Outside, LineStyleEnum.Thick);
			this.m_grid.Range(1, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).set_Borders(EdgeEnum.Inside, LineStyleEnum.Thin);
			return true;
		}

		private bool SetDataToGridDown()
		{
			if (this.m_lstDicData.Count <= 0 || this.m_grid == null)
			{
				YJKMessageBox.Show("没有统计数据");
				return false;
			}
			int cols = 0;
			int num = 0;
			int num2 = 0;
			int nNameIndex = 0;
			int nSepcIndex = 0;
			int num3 = -1;
			if (this.m_gridSetInfo.Legend)
			{
				num3 = 2;
				cols = 8;
				num = 6;
				num2 = 7;
				nNameIndex = 3;
				nSepcIndex = 4;
			}
			else
			{
				cols = 7;
				num = 5;
				num2 = 6;
				nNameIndex = 2;
				nSepcIndex = 3;
			}
			this.m_grid.Range(1, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).DeleteByRow();
			int num4 = 0;
			foreach (Dictionary<int, List<string>> dictionary in this.m_lstDicData)
			{
				num4 += dictionary.Count;
			}
			this.m_grid.Rows = num4 + 3;
			this.m_grid.Cols = cols;
			this.DrawTitle(num4);
			int num5 = num4;
			int num6 = 1;
			foreach (Dictionary<int, List<string>> dictionary2 in this.m_lstDicData)
			{
				List<List<int>> list = base.GroupingDicData(nNameIndex, dictionary2);
				for (int i = 0; i < list.Count; i++)
				{
					List<int> list2 = list[i];
					base.SortData(nSepcIndex, dictionary2, ref list2);
					foreach (int key in list2)
					{
						List<string> list3 = dictionary2[key];
						if (list3.Count > 0)
						{
							int j = 1;
							while (j < list3.Count)
							{
								if (j == 1)
								{
									this.m_grid.Cell(num5, j).Text = num6.ToString();
									goto IL_2F4;
								}
								if (j == num3)
								{
									string text = list3[j];
									string legendByFamilyName = FamilyLegend.GetLegendByFamilyName(this.m_doc, text);
									text = Path.GetFileName(legendByFamilyName);
									try
									{
										this.m_grid.Images.Add(legendByFamilyName, text);
										if (this.m_grid.Images.Item(text) == null)
										{
											goto IL_30A;
										}
										this.m_grid.Cell(num5, j).SetImage(text);
										goto IL_2F4;
									}
									catch
									{
										goto IL_30A;
									}
								}
								if (j == num)
								{
									this.m_grid.Cell(num5, j).Text = Math.Ceiling(Convert.ToDouble(list3[j]) * (1.0 + (double)this.m_gridSetInfo.Margin / 100.0)).ToString("0");
									goto IL_2F4;
								}
								if (j != num2)
								{
									this.m_grid.Cell(num5, j).Text = list3[j];
									goto IL_2F4;
								}
								if (base.StringIsNumber(list3[j]))
								{
									this.m_grid.Cell(num5, j).Text = Convert.ToDouble(list3[j]).ToString("0.00") + "㎡";
									goto IL_2F4;
								}
								this.m_grid.Cell(num5, j).Text = list3[j];
								goto IL_2F4;
								IL_30A:
								j++;
								continue;
								IL_2F4:
								this.m_grid.Cell(num5, j).Alignment = AlignmentEnum.CenterCenter;
								goto IL_30A;
							}
							num5--;
							num6++;
						}
					}
				}
			}
			this.m_grid.Range(1, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).WrapText = true;
			this.m_grid.Range(1, 1, num4 + 1, this.m_grid.Cols - 1).FontName = this.m_gridSetInfo.FontName;
			this.m_grid.Range(1, 1, num4 + 1, this.m_grid.Cols - 1).FontSize = this.m_gridSetInfo.FontSize;
			for (int k = 1; k < this.m_grid.Rows; k++)
			{
				this.m_grid.Row(k).AutoFit();
			}
			this.m_grid.Range(1, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).set_Borders(EdgeEnum.Outside, LineStyleEnum.Thick);
			this.m_grid.Range(1, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).set_Borders(EdgeEnum.Inside, LineStyleEnum.Thin);
			return true;
		}

		private void DrawTitle(int nRows)
		{
			int num = 1;
			this.m_grid.Cell(nRows + 1, num).Text = "序号";
			if (this.m_gridSetInfo.Legend)
			{
				this.m_grid.Cell(nRows + 1, ++num).Text = "图例";
			}
			this.m_grid.Cell(nRows + 1, ++num).Text = "名称";
			this.m_grid.Cell(nRows + 1, ++num).Text = "规格";
			this.m_grid.Cell(nRows + 1, ++num).Text = "单位";
			this.m_grid.Cell(nRows + 1, ++num).Text = "数量";
			this.m_grid.Cell(nRows + 1, ++num).Text = "备注";
			this.m_grid.Cell(nRows + 2, 1).Text = this.m_strGridName;
			this.m_grid.Range(nRows + 2, 1, nRows + 2, num).Merge();
			this.m_grid.Cell(nRows + 2, 1).FontSize = this.m_gridSetInfo.TitleFontSize;
			this.m_grid.Range(nRows + 1, 1, nRows + 2, num).Alignment = AlignmentEnum.CenterCenter;
		}

		private Document m_doc;

		private List<Dictionary<int, SatatisticDataInfo>> m_lstDicElems;

		private FlexCell.Grid m_grid;

		private List<Dictionary<int, List<string>>> m_lstDicData = new List<Dictionary<int, List<string>>>();

		private string m_strGridName = "";

		private GridSetInfo m_gridSetInfo;
	}
}
